package com.wanmait.myhouse.dao;
import com.wanmait.myhouse.util.DBHelper;
import com.wanmait.myhouse.vo.Type;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.taglibs.standard.extra.spath.Step;

import java.sql.SQLException;
import java.util.List;
public class TypeDAO {
    public List<Type> FindAll(){//查找所有house
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,type_name typeName from t_type;";
        try {
            List<Type>typeList = queryRunner.query(sql,new BeanListHandler<>(Type.class));
            return typeList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    public Type FindByName(Type type){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,type_name as typeName from t_type where type_name=?";
        try {
            Type type1 = queryRunner.query(sql, new BeanHandler<>(Type.class),type.getTypeName());
            return type1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    public void addTypeName(Type type){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "insert into t_type(type_name) values(?)";
        try {
            queryRunner.update(sql,type.getTypeName());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void delteById(Type type){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "delete from t_type where id=?";
        try {
            queryRunner.update(sql,type.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
